<?php
include('header.php');
$str=<<<EOD
<script type="text/javascript">
function deletevideos()
{
	var checks = document.getElementsByTagName("input");
	var todelete = new Array();
	for(i = 0; i < checks.length; i++)
	{
		if(checks[i].checked)
		{
			var id = checks[i].getAttribute("id");
			todelete.push(id);
		}
	}
	var xmlHttp = new XMLHttpRequest();
	var jtodelete = JSON.stringify(todelete);
	xmlHttp.open("get", "delete.php?"+"json="+jtodelete, true);
	xmlHttp.onreadystatechange = function () {
	  if (xmlHttp.readyState == 4) {
	    if (xmlHttp.status == 200) {
	      window.location = "profile.php";
	    }
	};
	xmlHttp.send(null);
}

function retweet()
{
	var checks = document.getElementsByTagName("input");
	var toretweet = new Array();
	for(i = 0; i < checks.length; i++)
	{
		if(checks[i].checked)
		{
			var id = checks[i].getAttribute("id");
			toretweet.push(id);
		}
	}
	var xmlHttp = new XMLHttpRequest();
	var jtoretweet = JSON.stringify(toretweet);
	xmlHttp.open("get", "retweet.php?"+"json="+jtoretweet, true);
	xmlHttp.onreadystatechange = function () {
	  if (xmlHttp.readyState == 4) {
	    if (xmlHttp.status == 200) {
	      window.location = "profile.php";
	    }
	};
	xmlHttp.send(null);
}
</script>

<section class="default">

<article class="center">



EOD;

echo $str;

echo "<h2>Hello ".$_SESSION['username']."!</h2>";

$database = mysql_connect("localhost", "root");
if(!$database){
		echo "<br>Not Connected";
	} else {
		$query = "SELECT * FROM tweets WHERE username = '".$_SESSION['username']."' AND `deleted` != '1'";
		$select = mysql_select_db('streamtube', $database);
		$reply = mysql_query($query);
		$table = array();
   	 	while ($row = mysql_fetch_array($reply)) {
    		$table[] = $row;
    	}
    	echo '<article class="center">';
    	if(sizeof($table) > 0)
    	{		
    		echo '<h3>These are your videos:</h3>';
    		echo '<table><tr class="textbold"><td></td><td class="padcol">Author</td><td class="padcol">Video</td><td class="padcol">Time</td></tr>';
    		$i = 0;
    		foreach($table as $t)
    		{
    			$mod = $i % 2;
    			if($mod == 0)
    			{
					echo '<tr class="light"><td><input id="'.$t['id'].'" type="checkbox"></td><td>'.$t['author'].'</td><td>'.$t['title'].'</td><td>'.$t['time'].'</td>';
    			} else {
    				echo '<tr class="dark"><td><input id="'.$t['id'].'" type="checkbox"></td><td>'.$t['author'].'</td><td>'.$t['title'].'</td><td>'.$t['time'].'</td>';
    			}
			if($t['new']==1)
			  echo '<td><img src="../img/new.png"></td></tr>';
			if($t['favorite']==1)
			  echo '<td><img src="../img/star.png"></td></tr>'; 
    			$i = $i + 1;
    		}
    		echo "</table><br>";    		
			echo '<button onClick="deletevideos()">Delete selected</button>';
			echo '<button onClick="retweet()">Re-Tweet selected</button>';
    	} else {
    		echo '<h3>You do not have videos in your playlist.</h3>';
    	}
	}
mysql_close();
$str1=<<<EOD
</article>
</article>
</section>

EOD;

echo $str1;

include('footer.php');



?>
